package com.xintonghua.base.utils;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by hao on 2017/6/12.
 */
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int width, half;

    public SpacesItemDecoration(int width) {
        this.width = width;
        this.half = width / 2;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {

        outRect.top = 0;
        outRect.bottom = 0;
        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.left = 0;
            outRect.right = width;
        } else if (parent.getChildLayoutPosition(view) == 1) {
            outRect.left = half;
            outRect.right = half;
        } else if (parent.getChildLayoutPosition(view) == 2) {
            outRect.left = width;
            outRect.right = 0;
        }
    }
}
